package org.dy.db.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DBConnectionManager {

	static boolean isOpen = false;
	
	static String contextName = "";

	static DataSource ds = null;

	public static void init() {
		try {
			InitialContext ctx = new InitialContext();
			// chang your jini naming here
			// eg.
			// java:comp/env/jdbc/mysql
			ds = (DataSource) ctx.lookup(contextName);

		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void setOpen(boolean open, String conName) {
		isOpen = open;
		contextName = conName;
		init();
	}

	public static Connection getConnection() {
		if (isOpen) {
			if (ds != null) {
				try {
					return ds.getConnection();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return null;
	}


	public static void release() {
		isOpen = false;
	}

}
